home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2005 December / DPPCPRO1205.ISO / Essentials / Programming / Basic4GL / Setup Basic4GL v2.3.1.exe / $INSTDIR / Programs / Heightmap1.gb < prev    next >
Encoding:
Text File  |  2005-07-29  |  4.0 KB  |  129 lines

  1. ' Heightmap demo 1
  2. '
  3. ' Note: The heightmap demos are designed to follow on from the starfield demos.
  4. '       Certain things are not explained as thoroughly, if they have already been 
  5. '       covered in the starfield demos.
  6. '       Therefore I recommend reading through the starfield demos first.
  7. '
  8. ' Loading a heightmap
  9. '
  10. ' File format:
  11. '
  12. ' XSize
  13. ' YSize
  14. ' Row 1
  15. ' Row 2
  16. ' ...                     
  17. '
  18. ' Where:    XSize and YSize are integers (in text format)
  19. '           Row is a list of space separated floating point height values (in text format)
  20. '
  21. ' XSize and YSize MUST be a power of 2
  22. '
  23. ' E.g:
  24. ' 4
  25. ' 4
  26. ' 0.7 1.4 1.7 1.0
  27. ' 0.2 0.8 1.1 0.4
  28. '-0.4 0.3 0.8 0.0
  29. ' 0.1 0.6 1.7 0.7
  30.  
  31. const heightScale# = 10 ' Height map scale factor
  32. dim xSize, ySize
  33. dim xMask, yMask        ' bitmasks to apply to X and Y coordinates
  34. dim file                ' File handle
  35. dim x, y                ' Working variables
  36.  
  37. goto Start
  38.  
  39. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  40. ' Routines
  41. CheckError:
  42.     ' Check for file error
  43.     if FileError () <> "" then
  44.         print FileError ()
  45.         CloseFile (file)
  46.         end
  47.     endif
  48.     return
  49.  
  50. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  51. ' Main program stars here    
  52. Start:
  53.  
  54. ' Open file
  55. file = OpenFileRead ("Files\Hills01.dat")
  56.     ' Note: Basic4GL will only open files in the "files" subfolder of the folder
  57.     '       where the program is saved.
  58. gosub CheckError
  59.  
  60. ' Read X and Y size
  61. xSize = Val (ReadLine (file))
  62. ySize = Val (ReadLine (file))
  63. gosub CheckError
  64.     ' Note: ReadLine reads a line of text from the file
  65.     '       Val converts it into a numeric value
  66.  
  67. ' Now that size is known, we can dim our 2D array
  68. dim h#(xSize - 1)(ySize - 1)        
  69.     ' This is how Basic4GL declares 2D arrays. We want it to range from 0 to xSize - 1,
  70.     ' hence we put in xSize - 1 as the X size. (Likewise with the Y size.)
  71. xMask = xSize - 1
  72. yMask = ySize - 1
  73.  
  74. for y = 0 to ySize - 1
  75.     for x = 0 to xSize - 1
  76.         h#(x)(y) = val (ReadText (file, true)) * heightScale#
  77.         gosub CheckError
  78.             ' Note: ReadText skips past whitespace, and reads a string of characters
  79.             '       up to the next whitespace.
  80.             '       (The second parameter is whether to treat newlines as whitespace.)
  81.     next
  82. next          
  83. CloseFile (file)
  84.  
  85. ' Render heightmap to screen
  86. ' For now we will simply plot points at the heightmap positions
  87.  
  88. ' Not using the Z buffer at this point
  89. glDisable (GL_DEPTH_TEST)
  90.  
  91. while true
  92.     
  93.     ' Clear the screen
  94.     glClear (GL_COLOR_BUFFER_BIT)
  95.     
  96.     ' Draw the heightmap points
  97.     ' We will use:
  98.     '   X for the X position
  99.     '   Y for the Z position
  100.     ' and read the Y position from the heightmap
  101.     '
  102.     ' This means that the grid cells will be spaced 1 unit apart
  103.     
  104.     ' To make sure the points are drawn in a visible position, we will push them
  105.     ' 100 units into the screen (add -100 to Z).
  106.     ' We will also move them -xSize/2 units in the X direction to centre them.
  107.     ' We will do this using the "translate" TRANSFORMATION. (Translate means move.)    
  108.     ' Setup the translation 
  109.     glLoadIdentity ()               ' Clear any existing transformations
  110.     glTranslatef (-xSize/2, 0, -100)
  111.     ' Now any points drawn will be translated (moved) by this value first
  112.     
  113.     ' If we rotate the points around the X axis slightly it gives the effect
  114.     ' of us looking down onto the grid, (instead of looking from side on).
  115.     ' So setup a Rotate TRANSFORMATION.
  116.     glRotatef (20, 1, 0, 0)     ' Rotate 20 degrees around the (1, 0, 0) vector
  117.     ' Note: This transformation will be combined with the translate, and BOTH
  118.     '       will be applied to everything drawn below
  119.  
  120.     glBegin (GL_POINTS)
  121.     for x = 0 to xSize - 1
  122.         for y = 0 to ySize - 1        
  123.             glVertex3f (x, h#(x)(y), y)
  124.         next
  125.     next
  126.     
  127.     ' Show the result
  128.     SwapBuffers ()
  129. wend